package com.hankol.newretail.adapter.details

import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.hankol.newretail.R
import com.hankol.newretail.bean.goods_details.GoodsSpecBean
import com.hankol.newretail.bean.goods_details.Spec
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout

/**
 * @author: yuyang
 * @date: 2023/1/4
 * @Des：
 */
class GoodsSpecAdapter : BaseQuickAdapter<GoodsSpecBean, BaseViewHolder>(R.layout.item_norms_group) {
    private var mOnUpdateGoodsInfoCallBack: OnUpdateGoodsInfoCallBack? = null

    fun setOnUpdateGoodsInfoCallBack(mOnUpNormsInfoCallBack: OnUpdateGoodsInfoCallBack) {
        this.mOnUpdateGoodsInfoCallBack = mOnUpNormsInfoCallBack
    }

    override fun convert(helper: BaseViewHolder, item: GoodsSpecBean) {
        item.selectedValueId = item.defaultSku.valueId
        helper.setText(R.id.tv_norms_group_title, item.groupName)
        var tagLayout: TagFlowLayout = helper.getView(R.id.tf_spec_name)
        tagLayout.setMaxSelectCount(1)
        tagLayout.adapter = object : TagAdapter<Spec>(item.specList) {
            override fun getView(parent: FlowLayout?, position: Int, s: Spec): View? {
                val tv = LayoutInflater.from(mContext).inflate(R.layout.item_norms_child, parent, false) as TextView
                tv.text = s.value
                if (s.valueId == item.selectedValueId) {
                    tv.setBackgroundResource(R.drawable.shape_rc_15_solid_f54d53_20)
                    tv.setTextColor(ContextCompat.getColor(mContext, R.color.color_F54D53))
                } else {
                    if (s.haveStock) {
                        tv.setBackgroundResource(R.drawable.shape_rc_15_solid_f2f2f2)
                        tv.setTextColor(ContextCompat.getColor(mContext, R.color.color_666666))
                    } else {
                        tv.setBackgroundResource(R.drawable.shape_rc_15_solid_f2f2f2_50alph)
                        tv.setTextColor(ContextCompat.getColor(mContext, R.color.color_50_666666))
                    }
                }

                return tv
            }

        }

        tagLayout.setOnTagClickListener { view, position, parent ->
            if (item.specList[position].haveStock) {
                item.defaultSku = item.specList[position]
                if (item.specRelationMap.containsKey(item.specList[position].valueId.toString())){
                    var selectedRelationMap = item.specRelationMap[item.specList[position].valueId.toString()]!!
                    for (d in data) {
                        if (!d.specList.contains(item.defaultSku)) {
                            for (spec in d.specList) {
                                spec.haveStock = selectedRelationMap.contains(spec.valueId.toString())
                            }
                        }
                    }
                }
                setNewData(data)
                mOnUpdateGoodsInfoCallBack?.onUpdateGoodsInfo()
            }
            true
        }
    }

    interface OnUpdateGoodsInfoCallBack {
        fun onUpdateGoodsInfo()
    }
}